﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Surging.Core.SwaggerGen
{
    public class SchemaRegistryFactory : ISchemaRegistryFactory
    {
        private readonly JsonSerializerSettings _jsonSerializerSettings;
        private readonly SchemaRegistryOptions _schemaRegistryOptions;

        public SchemaRegistryFactory(
            IOptions<JsonOptions> mvcJsonOptionsAccessor,
            IOptions<SchemaRegistryOptions> schemaRegistryOptionsAccessor)
            : this(mvcJsonOptionsAccessor.Value.JsonSerializerOptions, schemaRegistryOptionsAccessor.Value)
        { }

        public SchemaRegistryFactory(
            System.Text.Json.JsonSerializerOptions JsonSerializerOption,
            SchemaRegistryOptions schemaRegistryOptions)
        {
            if (JsonSerializerOption.PropertyNameCaseInsensitive)
            {
                _jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
                _jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                _jsonSerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            }
            else
            {
                _jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
                _jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                _jsonSerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
            }
            //_jsonSerializerSettings = jsonSerializerSettings;
            _schemaRegistryOptions = schemaRegistryOptions;
        }

        public ISchemaRegistry Create()
        {
            return new SchemaRegistry(_jsonSerializerSettings, _schemaRegistryOptions);
        }
    }
}
